<?php
class bloglistpage extends page{
	public $userId;
	public $userNumber;
	public $userName;
	public $bloglists;
	public $blogId;
	//display only one blog handle ;1 means display one blog 
	public $oneBlog;
	//display blog list of user with the userid $profileId
	public $profileId;
	public $blogKindList;
	public $cPage;
	public $pageSize;
	public $previousBlog;
	public $nextBlog;
	public function __construct(){
		parent::__construct();
		$this->title="IT博客，magento，php，mysql，javascript，jquery";
		$this->userNumber = $_SESSION["userNumber"];
		$this->userId =$_SESSION["userId"];
		$this->userName =$_SESSION["userName"];
		$profileId =$_GET["userId"];
		$this->profileId = $profileId;
		$this->cPage = $_GET["cPage"];
		if ($this->cPage==""){
			$this->cPage = 1;
		}
		$this->pageSize = 10;//每页显示10条
		$blogId = $_GET["blogId"];
		$this->blogId = $blogId;
		$this->blogKind=$blogKind = $_GET["blogKind"];
		if ($profileId!=""){
			$this->bloglists = $this->getBlogList($blogKind,$profileId,$this->userId,$this->cPage,$this->pageSize);
		}
		else if($blogId!=""){
			$this->oneBlog=1;
			$this->bloglists = $this->getOneBlog($blogId);
			$this->title = $this->bloglists[0][0]["title"];
			$this->previousBlog = $this->getPreviousBlog($blogId);
			$this->nextBlog = $this->getNextBlog($blogId);
			$log=new Log();
			
		}else{
			$this->profileId=1;
			$this->title="飘零的博客，magento，php，mysql，javascript，jquery";
			$this->bloglists = $this->getBlogList($blogKind,1,2,$this->cPage,$this->pageSize);
		}
		$this->blogKindList = $this->getBlogKindList($profileId);		
	}
	public function getBlogList($blogKind,$userId,$ownerId,$cPage,$pageSize){
		$dao =new bloglistdao();
		$blogLists = $dao->goGetBlogList($blogKind,$userId,$ownerId,$cPage,$pageSize);
		return $blogLists;
	}
	public function getOneBlog($blogId){
		$dao =new bloglistdao();
		$blogLists = $dao->goGetOneBlog($blogId);
		return $blogLists;
	}
	public function getBlogKindList($userId){
		$dao =new bloglistdao();
		if ($userId==""){
			$userId=$this->profileId;
		}
		$kindList =$dao->gogetBlogKindList($userId);
		return $kindList;
	}
	/**
	 * 文章分页函数
	 *
	 * @param string $table 数据表
	 * 
	 * 
	 */
	public function blogPageList($table){
		$profileId = $this->profileId;
		$cPage = $this->cPage;
		$nextPage = $cPage+1;
		$prePage = $cPage-1;
		$blogKind = $_GET["blogKind"];
		$pageSize = $this->pageSize;
		$dao = new bloglistdao();
		$pageNumber = $dao->goGetBlogPageList($table,$profileId,$pageSize,$blogKind);
		
		$blogTotal=$pageNumber*$pageSize;
		$blogTotal=$dao->getBlogTotalNumber($table,$profileId,$blogKind);
		echo "<ul class='pageList'>";
		//上一页
		//echo $pageNumber;
		
		if ($prePage>0){
			if($blogKind){
				echo "<li class='pageListLiNurual'><a href=bloglist.php?userId=$profileId&cPage=$prePage&blogKind=$blogKind>[上一页]</a><li>";
			}else {
				echo "<li class='pageListLiNurual'><a href=bloglist.php?userId=$profileId&cPage=$prePage>[上一页]</a><li>";
			}
		}
		if($cPage>=10){
			$befPages = $cPage-5;
			$aftPages = $cPage+4;
			if($aftPages>$pageNumber){
				$chaoChu = $aftPages-$pageNumber;
				$befPages-=$chaoChu;
				$aftPages=$pageNumber;
			}
		}else{
			$befPages=1;
			$aftPages = $pageNumber;
			if($aftPages>10){
				$aftPages=10;
			}
		}
		for ($i=$befPages;$i<=$aftPages;$i++){
			if($cPage==$i){
				echo "<li class='pageListLiSp'>$i</li>";
			}else {
				
				if($blogKind){
					echo "<li class='pageListLiUrual'><a href=bloglist.php?userId=$profileId&cPage=$i&blogKind=$blogKind>[$i]</a></li>";
				}else{
					echo "<li class='pageListLiUrual'><a href=bloglist.php?userId=$profileId&cPage=$i>[$i]</a></li>";
				}
			}
		}
		
		//下一页
		if($nextPage<=$pageNumber){
			if($blogKind){
				echo "<li class='pageListLiNurual'><a href=bloglist.php?userId=$profileId&cPage=$nextPage&blogKind=$blogKind>[下一页]</a></li>";
			}else {
				echo "<li class='pageListLiNurual'><a href=bloglist.php?userId=$profileId&cPage=$nextPage>[下一页]</a></li>";
			}
		}
		echo "</ul>";
		echo "<ul class='pageList'><li class='pageListLiNurual'>Page:$cPage"."/".$pageNumber."&nbsp;&nbsp;$blogTotal Blogs</li><ul>";
	}
	public function getXMLBlogShortCut($str,$len){
		$str = "<?xml version='1.0' encoding='UTF-8' ?>$str";
		$xml = simplexml_load_string($str);
		$data = "";
		if ($xml!=""){
			foreach ($xml->children as $child){
				if($data == ""){
					$data = $child;
				}else {
					$data .= "。".$child;
				}
			}
			$data = substr($data,0,$len)."...";
			return $data;
		}else{
			return $str;
		}	
	}
	public function getBlogShortCut($str,$len){
		//$str ="<p><h3>dsfdsf</h3></p>";
		$pattern = "/(<[^>]+>)|(<[\/?][^>]+>)/i";
		$replaceStr = "";
		$str = preg_replace($pattern,$replaceStr,$str);
		$str = substr($str,0,$len)."......";
		$str ="<p class='blogShortCutP'>$str</p>";
		return $str;
	}
	public function getPreviousBlog($blogId){
		$dao = new bloglistdao();
		$owner = $this->getBlogOwner($blogId);
		$result = $dao->goGetPreviousBlog($blogId,$owner);
		return $result;
	}
	public function getNextBlog($blogId){
		$dao = new bloglistdao();
		$owner = $this->getBlogOwner($blogId);
		$result = $dao->goGetNextBlog($blogId,$owner);
		return $result;
		
	}
	public function getKindBlogNumbers($kind){
		$dao = new bloglistdao();
		$userId = $this->profileId;
		$blogId = $_GET["blogId"];
		$result = $dao->goGetKindBlogNumbers($kind,$userId,$blogId);
		return $result;
	}
	public function getBlogKindContent($kind){
		$dao = new bloglistdao();
		$userId = $this->profileId;
		$blogId = $_GET["blogId"];
		$result = $dao->goGetBlogKindContent($userId,$kind,$blogId);
		return $result;
	}
	public function getBlogOwner($blogId){
		$dao = new bloglistdao();
		$ownerId = $dao->goGetBlogOwner($blogId);
		return $ownerId;
		
	}
	
	/**
	 * get blog comment;
	 *
	 * @param int $blogId
	 */
	public  function getBlogComment($blogId){
		$dao = new bloglistdao();
		$comment = $dao ->goGetBlogComment($blogId);
		return $comment;
	}
	public function getUserInfo($usernumber){
		$dao = new bloglistdao();
		return $userInfo = $dao->goGetUserInfo($usernumber);
	}
	public function getBlogownerByBlogId($blogId){
		$dao = new bloglistdao();
		return $dao->goGetBlogownerByBlogId($blogId);
		
	}
	public function getBlogKindById($kindId){
		$dao = new bloglistdao();
		return $dao->getBlogKindById($kindId);
	}
		
}

?>